/**
 *
 * Copyright Pact Lab of H.I.T.
 *
 * Designed and Implemented by Grid Researching Group, 
 * Pact Lab, Harbin
 * 
 * This Project is part of the national 973 Project:
 * Internet Based Virtual Computing Environment
 *
 * http://pact518.hit.edu.cn
 * 
 * Author:       Meteor <meteorlxk@gmail.com> 
 * Copyright:    pact518 
 * Version:      1.0
 * Created:      2009-5-12 
 * LastModified: 2009-5-12
 */
package edu.hit.pact.pgse.crawler.store;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.logging.Log;

import edu.hit.pact.pgse.bean.RawWebPage;
import edu.hit.pact.pgse.util.Md5Sum;
import edu.hit.pact.pgse.util.Utilities;

/**
 * @author meteorlxk
 *
 */
public abstract class AbstractFileSystemStore {
	protected String CachePath = null;
	protected static final String CacheSuffix = "raw";
	private static  int folderNum = 0;
	
	protected Log logger;
	
	public AbstractFileSystemStore(Log logger) {
		this.logger = logger;
		this.CachePath = Utilities.getDay() + "_" + CacheSuffix;
	}
	
	protected String getCacheFilePath(URL url) {
		return CachePath + "/" + url.getHost() + "/";
	}
	
	protected String getCacheFilePath(URL url,String subitem){
		return CachePath + "/" + url.getHost() + "/" + subitem + "/";
	}
	
	protected String getCacheFileName(URL url) {
		return CachePath + "/" + url.getHost() + "/" + Md5Sum.getMd5LongSum(url.toString()) + "." + CacheSuffix;
	}
	
	protected String getCacheFileName(URL url,String subitem) {
		return CachePath + "/" + url.getHost() + "/" + subitem + "/"+ Md5Sum.getMd5LongSum(url.toString()) + "." + CacheSuffix;
	}
	
//	protected String getCacheFileName(URL url,String subitem,int pageno){
//		return CachePath + "/" + url.getHost() + "/" + subitem + "/"+ pageno + "." + CacheSuffix;
//	}
	
	protected String getCacheFileName(URL url,String subitem,String pageno){
		return CachePath + "/" + url.getHost() + "/" + subitem + "/"+ pageno + "." + CacheSuffix;
	}
	
	protected String getCacheFileNameWithoutSuffix(URL url) {
		return CachePath + "/" + url.getHost() + "/" + Md5Sum.getMd5LongSum(url.toString());
	}
	
	protected boolean deleteObject(URL url) {
		try {
			Utilities.deleteFile(getCacheFileName(url));
		} catch (Exception e) {
			logger.error("delete object with url :" + url + " meets exception", e);
			return false;
		}//end try
		
		return true;
	}
	
	protected Object loadObject(URL url) {	
		Object obj = null;
		
		try {
			String fileName = getCacheFileName(url);
			
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(
					fileName));
			
			obj= in.readObject();
			
			in.close();
		} catch (Exception e) {
			logger.error("load object with url :" + url + " meets exception",
					e);	
		}//end if;
		
		return obj;
	}
	
	protected boolean storeObject(String urlStr, Object obj) {
		try {
			URL url = new URL(urlStr);
			
			Utilities.createdDir(getCacheFilePath(url));
			String fileName = getCacheFileName(url);
			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName));
			out.writeObject(obj);
			out.close();
			
//			String contentName = getCacheFileNameWithoutSuffix(url);
//			PrintWriter contentWriter = new PrintWriter(new FileOutputStream(contentName+".txt", false), true);
//			RawWebPage page = (RawWebPage)obj;
//			contentWriter.append(page.getPageRawContent());
//			contentWriter.close();
		} catch (IOException e) {
			logger.error("store object meets exception", e);
			return false;
		}//end try
		
		return true;
	}
	
	
	//add by HCZ 2010.11.7
	protected boolean storeObject(String urlStr,String subitem, Object obj) {
		try {
			URL url = new URL(urlStr);
			
			Utilities.createdDir(getCacheFilePath(url,subitem));
			String fileName = getCacheFileName(url,subitem);
			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName));
			out.writeObject(obj);
			out.close();
			
		} catch (IOException e) {
			logger.error("store object meets exception", e);
			return false;
		}//end try
		
		return true;
	}
	//add by HCZ 2010.11.7
	protected boolean storeObject(String urlStr,String subitem, String  pageno,String obj) {
		
		URL url = null;
		try {
			url = new URL(urlStr);
		} catch (MalformedURLException e1) {
			return false;
		}

		Utilities.createdDir(getCacheFilePath(url, subitem));
		String fileName = getCacheFileName(url, subitem, pageno);
		FileWriter stream = null;
			try {
				stream = new FileWriter(fileName);
			} catch (IOException e1) {
			this.CachePath = this.CachePath+"_"+folderNum++;
			logger.error("maybe too many links and cannot create new folder");
			return false;
		}
		try {
			BufferedWriter out = new BufferedWriter(stream);
			//ObjectOutputStream out = new ObjectOutputStream(stream);
			out.write(urlStr +"\n");
			out.write(obj);
			out.close();

		} catch (IOException e) {
			logger.error("store object meets exception", e);
			return false;
		}// end try

		return true;
	}
}
